package server.users;

import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;

import server.database.AbstractTable;
import server.database.DatabaseColumn;
import server.database.IDatabaseTable;
import server.database.query.IQueryResult;

public class UsersTable extends AbstractTable
{
	private static final Logger log = Logger.getLogger(UsersTable.class);

	public static final String TABLE_NAME = "Users";

	public static final DatabaseColumn ID = new DatabaseColumn(IDatabaseTable.ID, INTEGER, Arrays.asList(AUTO_INCREMENT));
	public static final DatabaseColumn USERNAME = new DatabaseColumn("username", STRING, Arrays.asList(UNIQUE));
	public static final DatabaseColumn PASSWORD = new DatabaseColumn("password", STRING, Arrays.asList(NOT_NULL), true);
	public static final DatabaseColumn FIRST_NAME = new DatabaseColumn("first_name", STRING, Arrays.asList(NOT_NULL));
	public static final DatabaseColumn LAST_NAME = new DatabaseColumn("last_name", STRING, Arrays.asList(NOT_NULL));
	public static final DatabaseColumn ROLE = new DatabaseColumn("role", INTEGER, Arrays.asList(NOT_NULL));

	public final List<DatabaseColumn> m_lstColumns = Arrays.asList(ID, USERNAME, PASSWORD, FIRST_NAME, LAST_NAME, ROLE);
	public final List<DatabaseColumn> m_lstKey = Arrays.asList(ID);
	public final List<DatabaseColumn> m_lstUniqueKey = Arrays.asList(USERNAME);

	public IQueryResult login(String sUsername, String sPassword)
	{
		return select(Arrays.asList(USERNAME, PASSWORD), Arrays.asList(sUsername, sPassword), m_lstColumns, AND);
	}

	public boolean isAdmin(String sUsername)
	{
		IQueryResult queryResult = select(Arrays.asList(USERNAME, ROLE), Arrays.asList(sUsername, UsersServlet.ADMIN), m_lstColumns, AND);
		if (queryResult.getResult() == IQueryResult.SUCESS)
		{
			return true;
		}

		return false;
	}

	@Override
	public String getTableName()
	{
		return TABLE_NAME;
	}

	@Override
	public List<DatabaseColumn> getColumns()
	{
		return m_lstColumns;
	}

	@Override
	public List<DatabaseColumn> getPrimaryKey()
	{
		return m_lstKey;
	}

	@Override
	public List<DatabaseColumn> getUniqueKey()
	{
		return m_lstUniqueKey;
	}

	@Override
	protected void initTableSpecific()
	{
		// TODO - set here to create default user if no users exists
	}

}
